<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>d</title>
    <style>
        .box{
            width: 100px;
            height: 100px;
            border: 1px solid red;
        }
    </style>
</head>
<body>
    <button class="btn">点我接收</button>
    <div class="box"></div>
</body>
<script>
    var btn = document.querySelector(".btn")
    var box = document.querySelector(".box")

    btn.addEventListener('click',function(){
        var xhr = new XMLHttpRequest();

        //设置两秒超时
        xhr.timeout = 2000
        //设置超时回调
        xhr.ontimeout = function(){
            alert('请求超时')
        }
        xhr.open('GET','http://127.0.0.1:8000/delay')
        xhr.send();
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4){
                if(200 === xhr.status){
                    console.log(xhr.status);
                    console.log(xhr.statusText);
                    console.log(xhr.getAllResponseHeaders());
                    console.log(xhr.response);

                    box.innerHTML = xhr.response

                }else{
                    console.log(xhr.status);
                }
            }else{
                console.log('错误'+xhr.readyState);
            }
        }
    })
</script>
</html>